library(doBy);
Nw_disc_trip<-read.csv(file="w:/IMARES/IJmuiden/Afdeling/Projecten/Kabeljauwmonitoring/Analyse/Eindrapport/sasdata/DCF/v20121207/nw_disc_trip_v20121207.csv")
landingweight<-(read.csv(file="http://marcelr.googlecode.com/svn/trunk/landingweight_v20121207.csv"))
effort_overview<-(read.csv(file="http://marcelr.googlecode.com/svn/trunk/effort_overview_v20121207.csv"))
head(effort_overview)
#alleen gegevens t/m 2011
head(Nw_disc_trip); unique(Nw_disc_trip$year)
head(landingweight); unique(landingweight$year)
head(effort_overview); unique(effort_overview$year)
#werkt niet::: write.csv(effort_overview,file="http://marcelr.googlecode.com/svn/trunk/effort_overview_vtesting.csv")
d.cod<-Nw_disc_trip[Nw_disc_trip$ENGLISH_NAME=="Cod",]
#a<-length.weight[length.weight$scientific.name=="Gadus morhua",]$a; b<-length.weight[length.weight$scientific.name=="Gadus morhua",]$b
#Nw_disc_trip.cod$weight<-  a*((Nw_disc_trip.cod$class_length)^b)* Nw_disc_trip.cod$n_total_trip/1000
head(d.cod)
tail(d.cod)
a.cod.1<-unique(Nw_disc_trip[c("year","PGM_CODE","prog","ship","POWER","metier","week","quar","duration_tot")])
a.cod.2<-aggregate(list(w_total_trip=d.cod$w_total_trip),list(year=d.cod$year,PGM_CODE=d.cod$PGM_CODE,prog=d.cod$prog,ship=d.cod$ship,POWER=d.cod$POWER,metier=d.cod$metier,week=d.cod$week,quar=d.cod$quar,duration_tot=d.cod$duration_tot,ICES_CODE=d.cod$ICES_CODE),sum)
a.cod.3<-merge(a.cod.1,a.cod.2,all=T)
#orderBy(~ship +POWER+metier+week+quar+duration_tot,data=a.cod.3)
a.cod.3$ICES_CODE<-"COD"
a.cod.3[is.na(a.cod.3$w_total_trip),]$w_total_trip<-0
a.cod.3$w_total_trip<-round(a.cod.3$w_total_trip,2)
head(a.cod.3); dim(a.cod.3)
a.cod.3$key<-paste(a.cod.3$year,a.cod.3$ship,a.cod.3$week,sep="_")
dim(a.cod.3[a.cod.3$year==2012,])
eval(parse('',text= paste("load(file='s:/IMARES/IJmuiden/Afdeling/Projecten/Discards demersaal/Eflalo.15sep-30nov.2012.rda')",sep="") ))
#load(file=paste('s:/IMARES/IJmuiden/Afdeling/Projecten/bearedo/Consultancy/Data/viris.rda',sep="_"))
viris<-eflalo.2012
head(viris)
viris[is.na(viris$FT_LTIME),]
b.cod.1<-aggregate(list(WEIGHT=viris$LE_KG_COD),
list(ship=viris$VE_REF,
LE_GEAR=viris$LE_GEAR,
DEP_DATE=viris$FT_DDAT,
DEP_TIME=viris$FT_DTIME,
ARR_DATE=viris$FT_LDAT,
ARR_TIME=viris$FT_LTIME,
#MS=viris$LE_MSZ,
power=viris$VE_KW),sum)
head(b.cod.1)
b.cod.1$WEIGHT
b.cod.1$year <-as.numeric(substr(as.Date(b.cod.1$DEP_DATE,"%d/%m/%Y"),1,4))
b.cod.1$week<-0
b.cod.1[is.na(b.cod.1$WEIGHT),]$WEIGHT<-0
#b.cod.1[b.cod.1$year==2011,]$week1<-(as.numeric(floor((as.Date(b.cod.1[b.cod.1$year==2011,]$ARR_DATE,"%d/%m/%Y")+2-as.Date("01/04/2011", "%m/%d/%Y"))/7))+as.numeric(floor((as.Date(b.cod.1[b.cod.1$year==2011,]$DEP_DATE,"%d/%m/%Y")+2-as.Date("01/04/2011", "%m/%d/%Y"))/7)))/2
b.cod.1[b.cod.1$year==2011,]$week<-  as.numeric(floor((as.Date(b.cod.1[b.cod.1$year==2011,]$DEP_DATE,"%d/%m/%Y")+2-as.Date("01/04/2011", "%m/%d/%Y"))/7))
#b.cod.1[b.cod.1$year==2012,]$week1<-(as.numeric(floor((as.Date(b.cod.1[b.cod.1$year==2012,]$ARR_DATE,"%d/%m/%Y")+2-as.Date("01/04/2012", "%m/%d/%Y"))/7))+as.numeric(floor((as.Date(b.cod.1[b.cod.1$year==2012,]$DEP_DATE,"%d/%m/%Y")+2-as.Date("01/04/2012", "%m/%d/%Y"))/7)))/2
b.cod.1[b.cod.1$year==2012,]$week<-  as.numeric(floor((as.Date(b.cod.1[b.cod.1$year==2012,]$DEP_DATE,"%d/%m/%Y")+2-as.Date("01/04/2012", "%m/%d/%Y"))/7))
head(b.cod.1)
b.cod.1$key<-paste(b.cod.1$year,b.cod.1$ship,b.cod.1$week,sep="_")
effort.cod<-merge(a.cod.3,b.cod.1,all=F,by=c("key"))
effort.cod[c(1:17)]
key<-unique(effort.cod$key)
effort.fin.1<-effort.cod
head(effort.fin.1)
a.cod.3b<-a.cod.3[!a.cod.3$key %in% key,]
b.cod.1$key<-paste(b.cod.1$year,b.cod.1$ship,b.cod.1$week+1,sep="_")
effort.cod.b<-merge(a.cod.3b,b.cod.1,all=F,by=c("key"))
dim(effort.cod.b)
b.cod.1$key<-paste(b.cod.1$year,b.cod.1$ship,b.cod.1$week-1,sep="_")
effort.cod.c<-merge(a.cod.3b,b.cod.1,all=F,by=c("key"))
dim(effort.cod.c)
effort.cod.f<-rbind(effort.cod,effort.cod.b,effort.cod.c)

#write.csv(effort.cod.f,file="s:/sasdata/effort.cod.f.csv")
effort.cod.f<-read.csv(file="s:/sasdata/effort.cod.f2.csv")
effort.fin.1<-effort.cod.f[!is.na(effort.cod.f$WEIGHT),]
effort.fin.1$year<-as.numeric(substr(as.Date(effort.fin.1$ARR_DATE,"%d/%m/%Y"),1,4))
effort.fin.1$CATCH<-effort.fin.1$WEIGHT+effort.fin.1$w_total_trip
effort.fin.1$Dperc<-NA
effort.fin.1[effort.fin.1$WEIGHT>0,]$Dperc<-effort.fin.1[effort.fin.1$WEIGHT>0,]$w_total_trip/effort.fin.1[effort.fin.1$WEIGHT>0,]$WEIGHT*100
effort.fin.1$DH<-0.0
effort.fin.1[substr(effort.fin.1$DEP_TIME,2,2)==":",]$DH<-as.numeric(substr(effort.fin.1[substr(effort.fin.1$DEP_TIME,2,2)==":",]$DEP_TIME,1,1))+as.numeric(substr(effort.fin.1[substr(effort.fin.1$DEP_TIME,2,2)==":",]$DEP_TIME,3,4))/60
effort.fin.1[substr(effort.fin.1$DEP_TIME,3,3)==":",]$DH<-as.numeric(substr(effort.fin.1[substr(effort.fin.1$DEP_TIME,3,3)==":",]$DEP_TIME,1,2))+as.numeric(substr(effort.fin.1[substr(effort.fin.1$DEP_TIME,3,3)==":",]$DEP_TIME,4,5))/60
effort.fin.1$AH<-0.0
effort.fin.1[substr(effort.fin.1$ARR_TIME,2,2)==":",]$AH<-as.numeric(substr(effort.fin.1[substr(effort.fin.1$ARR_TIME,2,2)==":",]$ARR_TIME,1,1))+as.numeric(substr(effort.fin.1[substr(effort.fin.1$ARR_TIME,2,2)==":",]$ARR_TIME,3,4))/60
effort.fin.1[substr(effort.fin.1$ARR_TIME,3,3)==":",]$AH<-as.numeric(substr(effort.fin.1[substr(effort.fin.1$ARR_TIME,3,3)==":",]$ARR_TIME,1,2))+as.numeric(substr(effort.fin.1[substr(effort.fin.1$ARR_TIME,3,3)==":",]$ARR_TIME,4,5))/60
effort.fin.1$DAS<-as.numeric(as.Date(effort.fin.1$ARR_DATE,"%d/%m/%Y")-as.Date(effort.fin.1$DEP_DATE,"%d/%m/%Y"))+(effort.fin.1$AH-effort.fin.1$DH)/24
effort.fin.1$KWDAS<-effort.fin.1$DAS*effort.fin.1$POWER
effort.fin.1$CpUE<-effort.fin.1$CATCH/effort.fin.1$KWDAS
metiers<-unique(as.character(effort.fin.1$metier))
effort.fin.1$metier2<-NA
effort.fin.1[substr(effort.fin.1$metier,1,3)=="TBB",]$metier2<-"BT2"
effort.fin.1[effort.fin.1$metier=="OTB/OTT_DEF_70-99mm",]$metier2<-"TR2"
effort.fin.1[effort.fin.1$metier=="OTB/OTT_DEF_100-119mm",]$metier2<-"TR1C"
effort.fin.1[effort.fin.1$metier=="SSC_DEF_>=120mm",]$metier2<-"TR1A"
effort.fin.1[effort.fin.1$metier=="SSC_DEF_100-119mm",]$metier2<-"TR1C"
head(effort.fin.1)
metiers<-unique(as.character(effort.fin.1$metier2))
results<-list()
for (year in c(2011)) {
for (met in metiers) {
result<-list()
result$year<-year
#result$quar<-qur
result$met<-as.character(met)
result$N<-length(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year,]$CpUE)
result$Nul<-mean(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$WEIGHT==0,]$w_total_trip)
result$Nulerr<-sqrt(var(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$WEIGHT==0,]$w_total_trip))/length(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$WEIGHT==0,]$w_total_trip)
result$NNul<-mean(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$WEIGHT>0,]$w_total_trip)
result$NNulerr<-sqrt(var(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$WEIGHT>0,]$w_total_trip))/length(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$WEIGHT>0,]$w_total_trip)
result$Dperc<-round(mean(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$year==year&effort.fin.1$WEIGHT>0,]$Dperc,na.rm=T),3)
result$Dperr<-sqrt(var(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$year==year&effort.fin.1$WEIGHT>0,]$Dperc,na.rm=T))/length(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year&effort.fin.1$year==year&effort.fin.1$WEIGHT>0,]$Dperc)
result$Dperc2<-round(sum(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year,]$w_total_trip)/sum(effort.fin.1[effort.fin.1$metier2==met&effort.fin.1$year==year,]$WEIGHT)*100,2)
results<-rbind(results,as.data.frame(result))
}
}
result<-results[results$N>0,][]
#write.csv(result,file="s:/sasdata/perc.cod.peryearmm.csv")
graph<-read.csv(file="s:/sasdata/results graph.csv")
graph$met2<-NA
graph[graph$met=="BT2",]$met2<-as.character(paste(graph[graph$met=="BT2",]$met," ",sep=""))
graph[graph$met=="TR2",]$met2<-as.character(paste(graph[graph$met=="TR2",]$met," ",sep=""))
graph[is.na(graph$met2),]$met2<-as.character(graph[is.na(graph$met2),]$met)
graph$leg<-paste(substr(graph$sampl,1,1),graph$met2,substr(graph$year,4,4),graph$quar,sep="_")
graph2<-graph[c("leg","Dperc","Dperr")]
write.csv(graph2,file="s:/sasdata/graph2.csv")
